package handler;
import service.Service;
import bean.BaseData;
import bean.Checkerboard;
import bean.Client;
import bean.RequestData;
import bean.ResponseData;

import com.gmail.dengtao.joe.transport.handler.Handler;
import com.gmail.dengtao.joe.transport.session.IdleStatus;
import com.gmail.dengtao.joe.transport.session.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ServiceHandler implements Handler{
	private Client clientWhite=new Client();
	private Client clientBlack=new Client();
	private Checkerboard checkerboard=new Checkerboard();
	private Service service=new Service();
	private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	   
	@Override
	public void sessionCreated(Session session) throws Exception {
		System.out.println("sessionCreated:"+session);
	}

	@Override
	public void sessionOpened(Session session) throws Exception {
		if(clientWhite.getSession()==null){
			clientWhite.setSession(session);
			clientWhite.setStatus(Client.IDLE);
			ResponseData responseData=new ResponseData();	
			responseData.setCommand(BaseData.INTI);
			responseData.setIdentify(Client.CILENT_WHILE);
			responseData.setContent("Welecome white!");
			session.send(gson.toJson(responseData).getBytes());
		}
		else if(clientBlack.getSession()==null){
			clientBlack.setSession(session);
			clientBlack.setStatus(Client.IDLE);
			ResponseData responseData=new ResponseData();	
			responseData.setCommand(BaseData.INTI);
			responseData.setIdentify(Client.CILENT_BLACK);
			responseData.setContent("Welecome black!");
			session.send(gson.toJson(responseData).getBytes());
		}
		System.out.println("sessionOpened"+session);
	}

	@Override
	public void sessionClosed(Session session) throws Exception {
		if(session==clientWhite.getSession()){
			clientWhite=new Client();
		}
		if(session==clientBlack.getSession()){
			clientBlack=new Client();
		}
		checkerboard=new Checkerboard();
		
	}

	@Override
	public void sessionIdle(Session session, IdleStatus status)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void exceptionCaught(Session session, Throwable cause) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dataReceived(Session session, Object data) throws Exception {
		//System.out.println("Received:" + new String((byte[]) data));
		String json=new String((byte[]) data);
		RequestData requestData=gson.fromJson(json,RequestData.class);
		boolean isGameOver=service.buildResponse(requestData, clientWhite, clientBlack, checkerboard);
		this.sendMessage();
		if(isGameOver){
			checkerboard=new Checkerboard();
		}
		System.out.println(data);
		
	}

	@Override
	public void dataNotSent(Session session, Object data) throws Exception {
		System.out.println("dataNotSent");
		
	}

	@Override
	public void dataSent(Session session, Object data) throws Exception {
		System.out.println("dataSent:"+new String((byte[])data));
		
	}
	public void sendMessage(){
		if(clientWhite!=null&&clientWhite.getSession()!=null&&clientWhite.getResponseData()!=null){			
			clientWhite.getSession().send(gson.toJson(clientWhite.getResponseData()).getBytes());
		}
		if(clientBlack!=null&&clientBlack.getSession()!=null&&clientBlack.getResponseData()!=null){			
			clientBlack.getSession().send(gson.toJson(clientBlack.getResponseData()).getBytes());
		}
	}

}
